package xyz.gpgl.erp.logic.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import common.util.base.CommonUtil;
import common.util.base.Result;
import common.util.mybatis.plus.layui.Page;
import common.util.mybatis.plus.logic.LogicBaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xyz.gpgl.erp.logic.entity.LogicEmailInfo;
import xyz.gpgl.erp.logic.entity.LogicShares;
import xyz.gpgl.erp.logic.entity.LogicSharesPrice;
import xyz.gpgl.erp.logic.entity.dto.LogicSharesDto;
import xyz.gpgl.erp.logic.entity.emeus.LogicSharesEmeus;
import xyz.gpgl.erp.logic.entity.emeus.LogicSharesPriceEmeus;
import xyz.gpgl.erp.logic.entity.param.LogicSharesListDataParam;
import xyz.gpgl.erp.logic.entity.param.LogicSharesParam;
import xyz.gpgl.erp.logic.service.ILogicSharesPriceService;
import xyz.gpgl.erp.logic.service.ILogicSharesService;
import xyz.gpgl.erp.sys.service.impl.MyUserDetailServiceImpl;

import java.math.BigDecimal;

/**
 * <p>
 * 股票信息 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2020-11-01
 */
@Slf4j
@Controller
@RequestMapping("/logic/logic_shares")
public class LogicSharesController extends LogicBaseController<ILogicSharesService, LogicShares>{

    @Autowired
    private ILogicSharesService iLogicSharesService;

    @Autowired
    private ILogicSharesPriceService logicSharesPriceService;
    /**
     * 跳转的列表页面
     * @return 跳转页面
     */
    @RequestMapping("/listPage")
    public Object listPage() {
      return "logicShares_list";
    }

    /**
     * 列表页面表格数据
     * @param param 实体对象
     * @param page 分页对象
     * @return page 分页对象
     */
    @ResponseBody
    @RequestMapping("/listData")
    public Result listData(LogicSharesListDataParam param, Page page) {
        return Result.success(this.iLogicSharesService.listData(param,page));
    }

    /**
     * 跳转到修改页面
     * @param id 唯一主键
     * @param model Spring提供的绑定器
     * @return 修改页面
     */
    @RequestMapping("/editPage")
    public Object editPage(Integer id, Model model) {
        if(CommonUtil.isNotEmpty(id)) {
            LogicShares entity = this.iLogicSharesService.getById(id);
            if(CommonUtil.isNotEmpty(entity)) {
                MyUserDetailServiceImpl.MyUserDetails user = (MyUserDetailServiceImpl.MyUserDetails) SecurityContextHolder
                        .getContext().getAuthentication().getPrincipal();
                QueryWrapper<LogicSharesPrice> priceQuery = new QueryWrapper<>();
                priceQuery.eq(LogicSharesPriceEmeus.username,user.getUsername());
                priceQuery.eq(LogicSharesPriceEmeus.sharesId,entity.getId());
                LogicSharesPrice price = this.logicSharesPriceService.getOne(priceQuery);
                LogicSharesDto edit = CommonUtil.copyBean(entity, LogicSharesDto.class);
                if (CommonUtil.isEmpty(price)){
                    edit.setBuyPrice(BigDecimal.ZERO);
                    edit.setSellPrice(BigDecimal.ZERO);
                }else {
                    edit.setBuyPrice(price.getBuyPrice());
                    edit.setSellPrice(price.getSellPrice());
                }
                model.addAttribute("entity",edit);
            }
        }
        return "logicShares_edit";
    }

    /**
     * 数据保存
     * @param entity 实体类
     * @return Result 统一结果对象
     */
    @ResponseBody
    @RequestMapping("/save")
    public Object save(@RequestBody LogicSharesParam entity) {

        if (CommonUtil.isEmpty(entity.getId())){
            if(this.iLogicSharesService.count(
                    new QueryWrapper<LogicShares>().eq(LogicSharesEmeus.code,entity.getCode()))
            >0){
                return Result.fail("股票编码已存在,请确认后输入");
            }
        }

        log.info("开始保存数据:{}" ,entity);
        this.iLogicSharesService.saveSharesInfo(entity);
        return Result.success();
    }

    /**
     * 检查 code 是否存在
     * @param code
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkCode")
    public Object checkCode(String code) {
        if (CommonUtil.isNotEmpty(code.trim())){
            if(this.iLogicSharesService.count(
                    new QueryWrapper<LogicShares>().eq(LogicSharesEmeus.code,code)
            )>0){
                return Result.fail("股票编码已存在,请确认后输入");
            }
        }

        return Result.success();
    }

    /**
     * 数据删除
     * @param entity 实体类
     * @return Result 统一结果对象
     */
    @ResponseBody
    @RequestMapping("/removed")
    public Object removed(LogicShares entity) {
        this.iLogicSharesService.removeById(entity.getId());
        return Result.success();
    }

    /**
     * 股票发送的邮箱对象
     */
    @ResponseBody
    @RequestMapping("/userEmail")
    public Result userEmail(Integer id,Page<LogicEmailInfo> page) {
        if (CommonUtil.isNotEmpty(id)){
            this.iLogicSharesService.userEmail(id,page);
        }
        return Result.success(page);
    }

    @RequestMapping("/addEmailUser")
    public String addEmailUser(Integer id, Model model) {
        model.addAttribute("id", id);
        return "shares_user_add";
    }
    @RequestMapping("/importView")
    public String importView() {
        return "shares_user_import_view";
    }

    @RequestMapping(value = "/importShares")
    public Result importShares(MultipartFile file) {
        this.iLogicSharesService.importShares(file);
        return  Result.success();
    }

}
